﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.Model;


namespace WebApp.admin.basic
{
    public partial class region : BaseAdmin
    {
        // Fields
        tbRegion oRegion = new tbRegion();


        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {

                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oRegion = BaseFactory.theRegion.GetModel(int.Parse(base.Request.QueryString["id"]));
                    this.oRegion.regionName = this.txtRegionName.Value;
                    this.oRegion.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    if (BaseFactory.theRegion.Update(this.oRegion))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"regionList.aspx?pid=" + this.oRegion.parentRegion + "\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                    }
                }
                else
                {
                    this.oRegion.CompanyId = int.Parse(base.Administrator.CompanyId);
                    this.oRegion.parentRegion = Request.QueryString["pid"] == "" ? 0 : int.Parse(Request.QueryString["pid"]);
                    this.oRegion.regionName = this.txtRegionName.Value.Trim();
                 
                    this.oRegion.CompanyId = int.Parse(base.Administrator.CompanyId);
                    if (BaseFactory.theRegion.Add(this.oRegion) > 0)
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"regionList.aspx?pid=" + this.oRegion.parentRegion + "\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                    }
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
        }

        public StringBuilder GetBcakLocation()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if (base.Request.QueryString["id"] != null)
                {
                    tbRegion regionById = BaseFactory.theRegion.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                    if (regionById != null)
                    {
                        builder.Append("location.href='regionList.aspx?pid=" + regionById.parentRegion + "';");
                    }
                    return builder;
                }
                if (base.Request.QueryString["pid"] != null)
                {
                    builder.Append("location.href='regionList.aspx?pid=" + base.Request.QueryString["pid"] + "';");
                }
            }
            catch
            {
            }
            return builder;
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
                {
                    builder.Append("编辑地区");
                    return builder;
                }
                if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["pid"] != null))
                {
                    builder.Append("添加地区");
                }
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            //{
            //    base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            //}
            if (!base.IsPostBack)
            {
                if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
                {
                    this.RegionBind();
                }
                else if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["pid"] != null))
                {
                    if (Convert.ToInt32(base.Request.QueryString["pid"]) == 0)
                    {
                        this.lblParentRegion.InnerText = "顶级地区";
                    }
                    else
                    {
                        tbRegion regionById = BaseFactory.theRegion.GetModel(Convert.ToInt32(base.Request.QueryString["pid"]));
                        if (regionById != null)
                        {
                            this.lblParentRegion.InnerText = regionById.regionName;
                        }
                    }
                }
            }
        }

        private void RegionBind()
        {
            try
            {
                tbRegion regionById = BaseFactory.theRegion.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (regionById != null)
                {
                    if (regionById != null)
                    {
                        tbRegion table2 = BaseFactory.theRegion.GetModel(Convert.ToInt32(regionById.parentRegion));
                        if (table2 != null)
                        {
                            this.lblParentRegion.InnerText = table2.regionName;
                        }
                    }
                    else
                    {
                        this.lblParentRegion.InnerText = "顶级地区";
                    }
                    this.txtRegionName.Value = regionById.regionName;
                }
            }
            catch
            {
            }
        }
    }
}